package com.ruoyi.copy;

import com.alibaba.fastjson2.JSON;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayTest {
    public static void main(String[] args) {
//        copySelf();
//        copyToOther();
//        getIntegerArrayFromObjectArray();
//        getStringArrayFromObjectArray1();
//        getStringArrayFromObjectArray2();
//        twoArray();
//        beanCopy();
        listCopy();
    }

    private static void copySelf() {
        int[] ids = {1, 2, 3, 4, 5};
        System.out.println(Arrays.toString(ids));
        //System.arraycopy(src, srcPos, dest, destPos, length);
        // 把从索引0开始的2个数字复制到索引为3的位置上
        System.arraycopy(ids, 0, ids, 3, 2);
        System.out.println(Arrays.toString(ids));//[1, 2, 3, 1, 2]
    }

    private static void copyToOther() {
        int[] ids = {1, 2, 3, 4, 5};
        //将数据的索引1开始的3个数据复制到目标的索引为0的位置上
        int[] other = new int[5];
        System.arraycopy(ids, 1, other, 0, 3);
        System.out.println(Arrays.toString(ids));//[1, 2, 3, 4, 5]深复制
        System.out.println(Arrays.toString(other));//[2, 3, 4, 0, 0]
    }

    //如果是类型转换问题，获取整形  必须从第一个String类型开始复制，不然会报错
    private static void getIntegerArrayFromObjectArray() {
        Object[] obj1 = {1, 2, 3, "4", "5"};
        Integer[] obj2 = new Integer[5];

        try {
            System.arraycopy(obj1, 0, obj2, 0, obj1.length);
        } catch (Exception e) {
            System.out.println("transfer exception:" + e);
        }
        System.out.println(Arrays.toString(obj1));
        System.out.println(Arrays.toString(obj2));
    }

    //获取Object数组中的字符串类型数据
    private static void getStringArrayFromObjectArray1() {
        Object[] obj3 = {1, 2, 3, "4", "5"};
        String[] obj4 = new String[5];
        try {
            System.arraycopy(obj3, 2, obj4, 2, 3);
        } catch (Exception e) {
            //transfer exception:java.lang.ArrayStoreException
            System.out.println("transfer exception:" + e);
        }
        System.out.println(Arrays.toString(obj3));
        //[null, null, null, null, null]
        System.out.println(Arrays.toString(obj4));
    }

    //获取Object数组中的字符串类型数据  String是深拷贝
    private static void getStringArrayFromObjectArray2() {
        Object[] obj3 = {1, 2, 3, "4", "5"};
        String[] obj4 = new String[5];
        try {
            System.arraycopy(obj3, 3, obj4, 3, 2);
        } catch (Exception e) {
            System.out.println("transfer exception:" + e);
        }
        System.out.println(Arrays.toString(obj3));
        //[null, null, null, 4, 5]
        System.out.println(Arrays.toString(obj4));
        obj3[3] = "zhangssan";
        System.out.println("查看是浅复制还是深复制~~~~~");
        System.out.println(Arrays.toString(obj3));
        System.out.println(Arrays.toString(obj4));
    }

    //二维数组  二维数组属于浅拷贝，原始数组改变后，复制的数据也发生了改变！
    public static void twoArray() {
        int[] arr1 = {1, 2};
        int[] arr2 = {3, 4};
        int[] arr3 = {5, 6};

        int[][] src = new int[][]{arr1, arr2, arr3};

        print("原始模样：", src);
        int[][] dest = new int[3][];
        System.arraycopy(src, 0, dest, 0, 3);

        System.out.println("改变前");
        print("src = ", src);
        print("dest = ", dest);

        //原数组改变后观察新数组是否改变，改变->浅复制，不改变->深复制
        src[0][0] = -1;

        System.out.println("改变后");
        print("src = ", src);
        print("dest = ", dest);
    }

    //二维数组toString()
    private static void print(String string, int[][] arr) {
        System.out.print(string);
        for (int[] a : arr) {
            for (int i : a) {
                System.out.print(i + " ");
            }
            System.out.print(",");
        }
        System.out.println();
    }

    /*对象数组 是深拷贝*/
    private static void beanCopy() {
        User user1 = new User("zs", 18);
        User user2 = new User("zc", 25);
        User user3 = new User("ww", 16);
        User[] userSrc = {user1, user2, user3};
        User[] userDest = new User[3];
        System.out.println("原始" + Arrays.toString(userSrc));
        System.arraycopy(userSrc, 0, userDest, 0, 3);
        System.out.println("userDest" + Arrays.toString(userDest));
        System.out.println("改变后");
        userSrc[0] = new User("ls", 95);
        userSrc[0].setName("ls");
        userSrc[0].setAge(95);
        System.out.println("原始" + Arrays.toString(userSrc));
        System.out.println("userDest" + Arrays.toString(userDest));

    }

    //ArrayList 使用 addAll 来拷贝数组  addAll是浅拷贝
    private static void listCopy() {
        User user1 = new User("zs", 18);
        User user2 = new User("zc", 25);
        User user3 = new User("ww", 16);
        ArrayList<User> list1 = new ArrayList<>();
        list1.add(user1);
        list1.add(user2);
        list1.add(user3);
        ArrayList<User> list2 = new ArrayList<>();
        System.out.println("原始" + JSON.toJSONString(list1));
        list2.addAll(list1);
//        System.arraycopy(list1, 0, list2, 0, 3);
        System.out.println("userDest" + JSON.toJSONString(list2));
        System.out.println("改变后");
//        user1 = new User("ls", 95);
        user1.setName("ls");
        user1.setAge(95);
        System.out.println("原始" + JSON.toJSONString(list1));
        System.out.println("userDest" + JSON.toJSONString(list2));

    }
}

